package com.fable.common.vo;

import com.fable.common.eunm.AppHttpCodeEnum;
import lombok.Data;

import java.io.Serializable;

@Data
public class ResultVO <T> implements Serializable {
    /**
     * 状态码
     */
    private Integer code;
    /**
     * 状态码信息
     */
    private String msg;
    /**
     * 具体信息
     */
    private T data;

    public ResultVO() {
        this.code = AppHttpCodeEnum.SUCCESS.getCode();
        this.msg = AppHttpCodeEnum.SUCCESS.getMsg();
    }

    public ResultVO(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public ResultVO(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResultVO(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static ResultVO errorResult(int code, String msg) {
        ResultVO result = new ResultVO();
        return result.error(code, msg);
    }
    public static ResultVO okResult() {
        ResultVO result = new ResultVO();
        return result;
    }
    public static ResultVO okResult(int code, String msg) {
        ResultVO result = new ResultVO();
        return result.ok(code, null, msg);
    }


    public static ResultVO okResult(Object data) {
        ResultVO result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getMsg());
        if(data!=null) {
            result.setData(data);
        }
        return result;
    }

    public static ResultVO errorResult(AppHttpCodeEnum enums){
        return setAppHttpCodeEnum(enums,enums.getMsg());
    }

    public static ResultVO errorResult(AppHttpCodeEnum enums, String msg){
        return setAppHttpCodeEnum(enums,msg);
    }

    public static ResultVO setAppHttpCodeEnum(AppHttpCodeEnum enums){
        return okResult(enums.getCode(),enums.getMsg());
    }

    private static ResultVO setAppHttpCodeEnum(AppHttpCodeEnum enums, String msg){
        return okResult(enums.getCode(),msg);
    }

    public ResultVO<?> error(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        return this;
    }

    public ResultVO<?> ok(Integer code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }

    public ResultVO<?> ok(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        return this;
    }

    public ResultVO<?> ok(T data) {
        this.data = data;
        return this;
    }
}
